﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logging.Util;
using System.Web.SessionState;

namespace Logging
{
    public class LogManager
    {
        [ThreadStatic]
        private static LogManager instance = null;

        public static LogManager GetInstance()
        {
            if (instance == null)
            {
                instance = new LogManager();
            }
            return instance;
        }

        private ILogger logger = null;

        public ILogger GetLogger()
        {
            if (logger == null)
            {
                logger = new Logger();
            }
            return logger;
        }

        #region 预留方法
        public ILogger GetLogger(string loggerName)
        {
            if (logger == null)
            {
                logger = new Logger();
                logger.Name = loggerName;
            }
            return logger;
        }

        public ILogger GetLogger(object obj)
        {
            if (logger == null)
            {
                logger = new Logger();
                logger.Name = obj.GetType().Name;
            }
            return logger;
        }

        #endregion

        private StopWatch stopWatch;

        StringBuilder builder = new StringBuilder();

        StringBuilder transactionLogs = new StringBuilder();
                
        private Exception exception;

        private HttpSessionState session;

        public HttpSessionState Session
        {
            get { return session; }
            set { session = value; }
        }

        public Exception Exception
        {
            get { return exception; }
            set { exception = value; }
        }

        public void Log(string message)
        {
            builder.AppendLine(message);
        }

        /// <summary>
        /// 获取所有日志
        /// </summary>
        /// <returns></returns>
        public string GetAllLogs()
        {
            return GetLogger().GetLog();
        }

        public void StartStopWatch()
        {
            stopWatch = new StopWatch();
        }

        public StopWatch GetStopWatch()
        {
            return stopWatch ?? new StopWatch();
        }

        public void Clear()
        {
            GetLogger().Clear();
            exception = null;
            session = null;
        }

    }
}
